import java.util.Arrays;

public class Test {
    public static int removeDuplicates(int[] nums) {
        int p1=0;
        int p2=1;
        while (p2!=nums.length) {
            if (nums[p1] != nums[p2]) {
                nums[++p1] = nums[p2];
            }
            p2++;
        }
        return p1+1;
    }
//    public static boolean containsDuplicate(int[] nums) {
//    //查找是否存在同一元素
//        int[]array=new int[1000000];
//        int p=nums.length;
//        for (int i = 0; i < nums.length; i++) {
//            int tmp=nums[i];
//            array[tmp]++;
//            if (array[tmp]==2) {
//                return true;
//            }
//        }
//        return false;
//    }
public static boolean containsDuplicate(int[] nums) {

        Arrays.sort(nums);
    for (int i = 0; i < nums.length-1; i++) {
        if (nums[i]==nums[i+1]) {
            return true;
        }
    }

        return false;
    }
    public static boolean findNumberIn2DArray(int[][] matrix, int target) {
    //二维数组查找

        int x=0;
        int y=matrix[0].length-1;
        while (x<matrix.length&&y+1!=0) {
            if (matrix[x][y]>target) {
                y--;
            }
           else if (matrix[x][y]<target) {
                x++;
            }
            else {
                return true;
            }
        }
        return false;
    }

    public static int findRepeatNumber(int[] nums) {
        int i=0;
        while (i<nums.length) {

            if (nums[i]==i) {
                i++;
                continue;
            }
            int tmp=nums[i];
            if (nums[tmp]==tmp) {
                return tmp;
            }
            nums[i]=nums[tmp];
            nums[tmp]=tmp;
        }
        return -1;

    }
    public static void main(String[] args) {
        int[]array={3,4,2,1,1,0};
        System.out.println(findRepeatNumber(array));
    }
    public static int findRepeatNumber1(int[] nums) {
        int[]array=new int[nums.length+1];
        for (int i = 0; i < nums.length; i++) {
            int tmp=nums[i];
            array[tmp]++;
            if (array[tmp]>1) {
                return tmp;
            }
        }
return -1;
    }

    public static void main2(String[] args) {
        int []array={1,2,3,1};
        System.out.println(containsDuplicate(array));
    }
    public static void main1(String[] args) {
        int[] array={1,2,2,3,4,4,6,6};
        System.out.println(removeDuplicates(array));
        System.out.println(Arrays.toString(array));
    }
}
